﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using OA.Core.Security;
using OA.Model.DB;
using OA.Model.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OA.Model
{
    public class MyDbContext : DbContext
    {
        public static IConfiguration Configuration;

        public MyDbContext() : base()
        {

        }

        public MyDbContext(DbContextOptions<MyDbContext> options)
          : base(options)
        {

        }

        /// <summary>
        /// 链接数据库的时候需要做的一些配置
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            
            if (!optionsBuilder.IsConfigured)
            {
                if (!VoucherDbContext.DicSql.ContainsKey(Configuration.GetConnectionString("MsSQL")))
                {
                    string sqlConnectStr = DESHeper.DecryptDES(Configuration.GetConnectionString("MsSQL"), "Cjy20088");
                    VoucherDbContext.DicSql.TryAdd(Configuration.GetConnectionString("MsSQL"), sqlConnectStr);
                }
                optionsBuilder.UseSqlServer(VoucherDbContext.DicSql[Configuration.GetConnectionString("MsSQL")])
                  .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

            }
        }

        /// <summary>
        /// 这里是配置实体和数据库之间的映射关系
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<VCustomers>(entity => { entity.ToView("V_Customers"); entity.HasKey("ID"); });
            modelBuilder.Entity<VHaveDone>(entity => { entity.ToView("V_HaveDone"); entity.HasKey("ID"); });
            modelBuilder.Entity<VWaitForHandling>(entity => { entity.ToView("V_WaitForHandling"); entity.HasKey("ID"); });
            modelBuilder.Entity<VEmployeePosition>(entity => { entity.ToView("V_EmployeePosition"); entity.HasKey("ID"); });
            modelBuilder.Entity<VLaunched>(entity => { entity.ToView("V_Launched"); entity.HasKey("ID"); });
            modelBuilder.Entity<VEmployeeInfo>(entity => { entity.ToView("V_EmployeeInfo"); entity.HasKey("EmpID"); });
            modelBuilder.Entity<EmployeeVoteView>(entity => { entity.ToView("V_EmployeeVoteView"); entity.HasKey("ID"); });
            modelBuilder.Entity<EmployeeTestPaperView>(entity => { entity.ToView("V_EmployeeTestPaper"); entity.HasKey("ID"); });
            modelBuilder.Entity<VKnowledge>(entity => { entity.ToView("V_Knowledge"); entity.HasKey("ID"); });

            modelBuilder.Entity<VDepartment>(entity => { entity.ToView("V_Department"); entity.HasKey("ID"); });
            modelBuilder.Entity<VProduct>(entity => { entity.ToView("V_Product"); entity.HasKey("ID"); });

            modelBuilder.Entity<VApprovalItem>(entity => { entity.ToView("V_ApprovalItem"); entity.HasKey("ID"); });

            modelBuilder.Entity<VPosition>(entity => { entity.ToView("V_Position"); entity.HasKey("ID"); });

            modelBuilder.Entity<VEmployeeNotice>(entity => { entity.ToView("V_EmployeeNotice"); entity.HasKey("ID"); });
            

            base.OnModelCreating(modelBuilder);

        }

        public virtual DbSet<VProduct> VProduct { get; set; }

        public virtual DbSet<VPosition> VPosition { get; set; }

        /// <summary>
        /// 小程序首页头部图片广告
        /// </summary>
        public virtual DbSet<ApiAD> ApiAD { get; set; }

        /// <summary>
        /// 便签
        /// </summary>
        public virtual DbSet<Note> Note { get; set; }

        public virtual DbSet<EmployeeTestPaper> EmployeeTestPaper { get; set; }

        public virtual DbSet<VEmployeePosition> VEmployeePosition { get; set; }

        public virtual DbSet<VEmployeeInfo> VEmployeeInfo { get; set; }

        public virtual DbSet<VEmpPermission> VEmpPermission { get; set; }

        public virtual DbSet<VDepartment> VDepartment { get; set; }

        public virtual DbSet<VKnowledge> VKnowledge { get; set; }

        public virtual DbSet<VEmployeeNotice> VEmployeeNotice { get; set; }


        /// <summary>
        /// 协同单据
        /// </summary>
        public virtual DbSet<SynergyForm> SynergyForm { get; set; }

        /// <summary>
        /// 我的待办
        /// </summary>
        public virtual DbSet<VWaitForHandling> VWaitForHandling { get; set; }


        /// <summary>
        /// 我的已办
        /// </summary>
        public virtual DbSet<VHaveDone> VHaveDone { get; set; }



        /// <summary>
        /// 员工投票
        /// </summary>
        public virtual DbSet<EmployeeVoteView> EmployeeVoteView { get; set; }

        /// <summary>
        /// 用户
        /// </summary>
        public virtual DbSet<Employee> Employee { get; set; }

        /// <summary>
        /// 配置
        /// </summary>
        public virtual DbSet<SysConfig> SysConfig { get; set; }


        /// <summary>
        /// 管理员
        /// </summary>
        public virtual DbSet<AdminInfo> AdminInfo { get; set; }


        /// <summary>
        /// 管理员角色
        /// </summary>
        public virtual DbSet<AdminPermission> AdminPermission { get; set; }

        /// <summary>
        /// 登陆日志
        /// </summary>
        public virtual DbSet<LoginLog> LoginLog { get; set; }


        /// <summary>
        /// 用户表单分组
        /// </summary>
        public virtual DbSet<FormGroup> FormGroup { get; set; }

        /// <summary>
        /// 用户表单
        /// </summary>
        public virtual DbSet<CustomForm> CustomForm { get; set; }

        /// <summary>
        /// 部门
        /// </summary>
        public virtual DbSet<Department> Department { get; set; }

        /// <summary>
        /// 岗位
        /// </summary>
        public virtual DbSet<Position> Position { get; set; }


        /// <summary>
        /// 职员岗位表
        /// </summary>
        public virtual DbSet<EmployeePosition> EmployeePosition { get; set; }

        /// <summary>
        /// 表单内容
        /// </summary>
        public virtual DbSet<Voucher> Voucher { get; set; }

        /// <summary>
        /// 审批内容
        /// </summary>
        public virtual DbSet<Approval> Approval { get; set; }


        public virtual DbSet<ApprovalItem> ApprovalItem { get; set; }

        public virtual DbSet<VApprovalItem> VApprovalItem { get; set; }

        /// <summary>
        /// 审批节点
        /// </summary>
        public virtual DbSet<ProcessPoint> ProcessPoint { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        public virtual DbSet<Mailbox> Mailbox { get; set; }

        /// <summary>
        /// 工作委托
        /// </summary>
        public virtual DbSet<MyDelegation> MyDelegation { get; set; }

        /// <summary>
        /// 投票
        /// </summary>
        public virtual DbSet<Vote> Vote { get; set; }


        /// <summary>
        /// 考试，测试
        /// </summary>
        public virtual DbSet<TestPaper> TestPapers { get; set; }


        public virtual DbSet<EmployeeTestPaperView> EmployeeTestPaperView { get; set; }


        /// <summary>
        /// 用户答卷
        /// </summary>
        public virtual DbSet<EmployeeTestPaperSelect> EmployeeTestPaperSelect { get; set; }


        /// <summary>
        /// 用户与投票表
        /// </summary>
        public virtual DbSet<EmployeeVote> EmployeeVote { get; set; }

        /// <summary>
        /// 用户投票选择项
        /// </summary>
        public virtual DbSet<EmployeeVoteSelect> EmployeeVoteSelect { get; set; }


        /// <summary>
        /// 通知公告
        /// </summary>
        public virtual DbSet<Notice> Notice { get; set; }

        /// <summary>
        /// 用户通知公告表
        /// </summary>
        public virtual DbSet<EmployeeNotice> EmployeeNotice { get; set; }

        /// <summary>
        /// 考勤
        /// </summary>
        public virtual DbSet<Attendance> Attendance { get; set; }

        /// <summary>
        /// 工作日志
        /// </summary>
        public virtual DbSet<Diary> Diary { get; set; }

        /// <summary>
        /// 事务提醒
        /// </summary>
        public virtual DbSet<AffairAlert> AffairAlert { get; set; }

        /// <summary>
        /// 文件分组
        /// </summary>
        public virtual DbSet<FileGroup> FileGroup { get; set; }

        /// <summary>
        /// 文件
        /// </summary>
        public virtual DbSet<MyFile> MyFile { get; set; }

        /// <summary>
        /// 用户文件关联
        /// </summary>
        public virtual DbSet<EmployeeFile> EmployeeFile { get; set; }

        /// <summary>
        /// 员工与审批表单管理表
        /// </summary>
        public virtual DbSet<EmployeeCustomForm> EmployeeCustomForm { get; set; }

        /// <summary>
        /// 知识分组
        /// </summary>
        public virtual DbSet<KnowledgeGroup> KnowledgeGroup { get; set; }

        /// <summary>
        /// 知识
        /// </summary>
        public virtual DbSet<Knowledge> Knowledge { get; set; }

        /// <summary>
        /// 审批节点
        /// </summary>
        public virtual DbSet<ApprovalNode> ApprovalNode { get; set; }

        /// <summary>
        /// 权限组
        /// </summary>
        public virtual DbSet<PermissionGroup> PermissionGroup { get; set; }

        /// <summary>
        /// 用户权限
        /// </summary>
        public virtual DbSet<UserPermissionGroup> UserPermissionGroup { get; set; }

        /// <summary>
        /// 回调表
        /// </summary>
        public virtual DbSet<CallBackInfo> CallBackInfo { get; set; }


        public virtual DbSet<OperationLogs> OperationLogs { get ;set;}

        public virtual DbSet<ConfigInfo> ConfigInfo { get; set; }

        public virtual DbSet<VLaunched> VLaunched { get; set; }

        public virtual DbSet<RelatedCompany> RelatedCompany { get; set; }

        public virtual DbSet<RelatedCompanyContacts> RelatedCompanyContacts { get; set; }

        public virtual DbSet<ProductCategory> ProductCategory { get; set; }

        public virtual DbSet<Product> Product { get; set; }

        public virtual DbSet<RelatedCompanyProduct> RelatedCompanyProduct { get; set; }

        public virtual DbSet<TableInfo> TableInfo { get; set; }



    }
}
